﻿using CMS.DataAccess;
using CMS.Model;
using Newtonsoft.Json.Linq;
using System.Text.RegularExpressions;

namespace CMS.Utils
{
    public static class QueryConditionResolver
    {
        public static CondCollection Resolver<T>(dynamic qryParam) where T : BaseModel, new()
        {
            CondCollection ret = CondCollection.True();
            if (qryParam == null)
            {
                return ret;
            }
            var datAsDic = qryParam as JObject;
            if (datAsDic == null)
            {
                return ret;
            }
            var pils = typeof(T).GetProperties();
            foreach (var pi in pils)
            {
                if (datAsDic.ContainsKey("S_" + pi.Name))
                {
                    WhereCond cond = new WhereCond(pi.Name);
                    MatchValue(datAsDic["S_" + pi.Name].ToString(), cond);
                    ret.AddWhereCond(cond);
                }
                for (int i = 0; i < 8; i++)
                {
                    //同一搜索关键字支持最多8个不同的值作为条件，用于查找范围区间(ge,le)等场景
                    string key = "S_" + pi.Name + "_" + i;
                    if (datAsDic.ContainsKey(key))
                    {
                        WhereCond cond = new WhereCond(pi.Name);
                        MatchValue(datAsDic[key].ToString(), cond);
                        ret.AddWhereCond(cond);
                    }
                }
                if (datAsDic.ContainsKey("O_" + pi.Name))//按多个条件排序，不能指定排序的先后顺序
                {
                    OrderByField orderByField = new OrderByField();
                    orderByField.Field = pi.Name;
                    orderByField.Desc = datAsDic["O_" + pi.Name].ToString().ToUpper() == "DESC";
                    ret.OrderBy.Add(orderByField);
                }
            }
            return ret;
        }
        private static void MatchValue(string val, WhereCond conditional)
        {
            if (val == null) return;
            string reg1 = "\\([\\s\\S]*\\)$";
            if (Regex.IsMatch(val, "^eq" + reg1, RegexOptions.IgnoreCase))
            {
                conditional.Op = OpEnum.EqIgnoreCase;
                conditional.Value = val.Substring(3, val.Length - 4);
            }
            if (Regex.IsMatch(val, "^gt" + reg1, RegexOptions.IgnoreCase))
            {
                conditional.Op = OpEnum.Gt;
                conditional.Value = val.Substring(3, val.Length - 4);
            }
            if (Regex.IsMatch(val, "^lt" + reg1, RegexOptions.IgnoreCase))
            {
                conditional.Op = OpEnum.Lt;
                conditional.Value = val.Substring(3, val.Length - 4);
            }
            if (Regex.IsMatch(val, "^ge" + reg1, RegexOptions.IgnoreCase))
            {
                conditional.Op = OpEnum.Ge;
                conditional.Value = val.Substring(3, val.Length - 4);
            }
            if (Regex.IsMatch(val, "^le" + reg1, RegexOptions.IgnoreCase))
            {
                conditional.Op = OpEnum.Le;
                conditional.Value = val.Substring(3, val.Length - 4);
            }
            if (Regex.IsMatch(val, "^lk" + reg1, RegexOptions.IgnoreCase))
            {
                conditional.Op = OpEnum.Lk;
                conditional.Value = "%" + val.Substring(3, val.Length - 4) + "%";
            }
            if (Regex.IsMatch(val, "ne" + reg1, RegexOptions.IgnoreCase))
            {
                conditional.Op = OpEnum.NotEq;
                conditional.Value = val.Substring(3, val.Length - 4);
            }
        }
    }
}
